package com.artron.mediaartron.ui.linkpage;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.annotation.LayoutRes;
import android.util.Property;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.artron.mediaartron.R;
import com.artron.mediaartron.base.BaseLinkPage;

import butterknife.BindView;


/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-21.
 */

public class CustomSelectionPosterLinkPage extends BaseLinkPage implements View.OnClickListener {

    @BindView(R.id.CustomSelectionPosterLinkPage_iv_left)
    ImageView mIvLeft;
    @BindView(R.id.CustomSelectionPosterLinkPage_iv_right)
    ImageView mIvRight;
    @BindView(R.id.CustomSelectionPosterLinkPage_iv_center)
    ImageView mIvCenter;
    @BindView(R.id.CustomSelectionPosterLinkPage_iv_click_center)
    ImageView mIvClickCenter;
    @BindView(R.id.CustomSelectionPosterLinkPage_iv_prompt1)
    ImageView mIvPrompt1;
    @BindView(R.id.CustomSelectionPosterLinkPage_iv_prompt3)
    ImageView mIvPrompt3;
    @BindView(R.id.CustomSelectionPosterLinkPage_btn_know)
    Button mBtnKnow;
    private AnimatorSet mAnimatorOfLeftAndRight;

    private CustomSelectionPosterLinkPage(ViewGroup parent, @LayoutRes int layoutId) {
        super(parent, layoutId);
    }

    public CustomSelectionPosterLinkPage(ViewGroup parent) {
        this(parent, R.layout.layout_custom_selection_poster_link_page);
    }

    @Override
    protected void initView() {
        setFirstVisible(true);

        ObjectAnimator animator1 = initAnimator(mIvLeft, View.TRANSLATION_X, 0, 40.f);
        ObjectAnimator animator2 = initAnimator(mIvRight, View.TRANSLATION_X, 0, -40.f);
        ObjectAnimator animatorCenterX = initCenterAnimator(mIvCenter, View.TRANSLATION_X, -100, 100);
        mAnimatorOfLeftAndRight = new AnimatorSet();
        mAnimatorOfLeftAndRight.playTogether(animator1, animator2, animatorCenterX);
        mAnimatorOfLeftAndRight.start();


        mBtnKnow.setOnClickListener(this);
    }

    private ObjectAnimator initAnimator(View target, Property<View, Float> property,
                                        float... values) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, property, values);
        animator.setRepeatCount(1000);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        return animator;
    }

    private ObjectAnimator initCenterAnimator(View target, Property<View, Float> property,
                                              float... values) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, property, values);
        animator.setRepeatCount(1000);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setDuration(3000);
        return animator;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.CustomSelectionPosterLinkPage_btn_know:
                if (mIvLeft.getVisibility() == View.VISIBLE) {
                    setFirstVisible(false);
                    mAnimatorOfLeftAndRight.cancel();
                    mIvCenter.setTranslationX(0);
                    setSecondVisible(true);
                } else {
                    mContainer.removeView(mContentView);
                }

                break;
        }
    }

    private void setFirstVisible(boolean isLeftAndRight) {
        int visible = isLeftAndRight ? View.VISIBLE : View.GONE;
        mIvLeft.setVisibility(visible);
        mIvRight.setVisibility(visible);
        mIvPrompt1.setVisibility(visible);
    }

    private void setSecondVisible(boolean isShowClickPage) {
        int visible = isShowClickPage ? View.VISIBLE : View.GONE;
        int invisible = !isShowClickPage ? View.VISIBLE : View.GONE;
        mIvCenter.setVisibility(invisible);
        mIvClickCenter.setVisibility(visible);
        mIvPrompt3.setVisibility(visible);
    }
}
